package com.example.listening.fragment;

import java.util.Date;
import org.json.JSONObject;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageCache;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.bigkoo.svprogresshud.SVProgressHUD;
import com.example.common.JSONCallback;
import com.example.common.MyRelativeLayout;
import com.example.listening.BaseApplication;
import com.example.listening.HomeActivity;
import com.example.listening.R;
import com.example.listening.bean.PinYu;
import com.example.listening.utils.AllContacts;
import com.example.listening.utils.ObjectCacheToFile;
import com.example.listening.utils.Pentagon.PentagonUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import okhttp3.Call;

public class AlreadyBuyFragment extends Fragment {
	private ImageView mImageView_EveryDay;
	private TextView mTextView_EveryDay;
	private NetworkImageView mImageView_Notice;
	private View view;
	private Context mContext;
	private RequestQueue mQueue = null;
	private ImageLoader mImageLoader;
	private ImageView mImageView_Wubianxing;
	private Button mButton_Wubianxing;
	private boolean isLocal = false;
	private PinYu zuhePinYu;
	private TextView tv_xxmj;

	private MyRelativeLayout my_layout;
	private View ll_desc;

	private TextView tv_cihui;
	private TextView tv_cizu;
	private TextView tv_shitai;
	private TextView tv_juxing;
	private TextView tv_lijie;
	private SVProgressHUD progressHUD;
	private JSONObject lastData;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_alreadybuy, null);
		tv_xxmj = (TextView) view.findViewById(R.id.tv_xxmj);
		progressHUD = new SVProgressHUD(getContext());
		initView();
		setData();
		getLineView();
//		getLocalView();
		return view;
	}

	private void initView() {
		my_layout = (MyRelativeLayout) view.findViewById(R.id.my_layout);
//		my_layout.setOnFlingListener(new MyRelativeLayout.OnFlingListener() {
//			@Override
//			public void onFling(boolean left) {
//				if (left == isLocal) {
//					return;
//				}
//
//				if (isLocal) {
//					getLineView();
//					mButton_Wubianxing.setText("上一次");
//				} else {
//					getLocalView();
//					mButton_Wubianxing.setText("查看综合");
//				}
//				isLocal = !isLocal;
//			}
//		});
		ll_desc = view.findViewById(R.id.ll_desc);

		tv_cihui = (TextView) view.findViewById(R.id.tv_cihui);
		tv_cizu = (TextView) view.findViewById(R.id.tv_cizu);
		tv_shitai = (TextView) view.findViewById(R.id.tv_shitai);
		tv_juxing = (TextView) view.findViewById(R.id.tv_juxing);
		tv_lijie = (TextView) view.findViewById(R.id.tv_lijie);

		zuhePinYu = new PinYu();
		mContext = getActivity();
		mImageView_EveryDay = (ImageView) view
				.findViewById(R.id.image_everyday);
		mTextView_EveryDay = (TextView) view.findViewById(R.id.text_everyday);
		mImageView_Notice = (NetworkImageView) view
				.findViewById(R.id.notice_imageview);
		mImageView_Wubianxing = (ImageView) view
				.findViewById(R.id.image_wubianxing);
		mButton_Wubianxing = (Button) view.findViewById(R.id.button_wubianxing);
		mButton_Wubianxing.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (isLocal) {
					getLineView();
					mButton_Wubianxing.setText("上一次");
				} else {
					getLocalView();
					mButton_Wubianxing.setText("查看综合");
				}
				isLocal = !isLocal;
			}
		});
		mQueue = Volley.newRequestQueue(mContext);
		mImageLoader = new ImageLoader(mQueue, new ImageCache() {
			@Override
			public void putBitmap(String url, Bitmap bitmap) {
			}

			@Override
			public Bitmap getBitmap(String url) {
				return null;
			}
		});
	}

	private void getLocalView() {
		Message message = new Message();
		HomeActivity activity = (HomeActivity) getActivity();
		message.what = 1;
		activity.mHandler.sendMessage(message);
	}

	private void getLineView() {
		JSONCallback callback = new JSONCallback() {
			@Override
			public void onError(Call call, Exception e, int id) {
				progressHUD.dismiss();
				Toast.makeText(getContext(),"数据获取失败",Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onResponse(JSONObject json, int id) {
				progressHUD.dismiss();
				if (!isLocal) {
					if (json.opt("status").toString().trim().equals("0")) {
						JSONObject dataJson = json.optJSONObject("data");
						if (null != dataJson) {
							int s1_right = Integer.parseInt(String
									.valueOf(dataJson.opt("s1_right")));
							int s2_right = Integer.parseInt(String
									.valueOf(dataJson.opt("s2_right")));
							int s3_right = Integer.parseInt(String
									.valueOf(dataJson.opt("s3_right")));
							int s4_right = Integer.parseInt(String
									.valueOf(dataJson.opt("s4_right")));
							int s5_right = Integer.parseInt(String
									.valueOf(dataJson.opt("s5_right")));
							int s1_count = Integer.parseInt(String
									.valueOf(dataJson.opt("s1_count")));
							int s2_count = Integer.parseInt(String
									.valueOf(dataJson.opt("s2_count")));
							int s3_count = Integer.parseInt(String
									.valueOf(dataJson.opt("s3_count")));
							int s4_count = Integer.parseInt(String
									.valueOf(dataJson.opt("s4_count")));
							int s5_count = Integer.parseInt(String
									.valueOf(dataJson.opt("s5_count")));
							double s1 = 0.000000;
							if (s1_count != 0) {
								s1 = (double) (s1_right * 10000 / s1_count) / 10000.00000;
							}
							zuhePinYu.cihui = AllContacts
									.getComment((int) (s1 * 100));
							double s2 = 0.000000;
							if (s2_count != 0) {
								s2 = (double) (s2_right * 10000 / s2_count) / 10000.00000;
							}
							zuhePinYu.cizu = AllContacts
									.getComment((int) (s2 * 100));
							double s3 = 0.000000;
							if (s3_count != 0) {
								s3 = (double) (s3_right * 10000 / s3_count) / 10000.00000;
							}
							zuhePinYu.shitai = AllContacts
									.getComment((int) (s3 * 100));
							double s4 = 0.000000;
							if (s4_count != 0) {
								s4 = (double) (s4_right * 10000 / s4_count) / 10000.00000;
							}
							zuhePinYu.juxing = AllContacts
									.getComment((int) (s4 * 100));
							double s5 = 0.000000;
							if (s5_count != 0) {
								s5 = (double) (s5_right * 10000 / s5_count) / 10000.00000;
							}
							zuhePinYu.lijie = AllContacts
									.getComment((int) (s5 * 100));
							Double[] arr = { s1 * 100, s2 * 100, s3 * 100,
									s4 * 100, s5 * 100 };
							Message message = new Message();
							HomeActivity activity = (HomeActivity) getActivity();
							message.what = 0;
							message.obj = arr;
							activity.mHandler.sendMessage(message);
						}
					}
				}
				AlreadyBuyFragment.this.lastData = json;
			}
		};

		if (lastData == null) {
			progressHUD.show();
		}
		else {
			callback.onResponse(lastData,-1);
		}
		OkHttpUtils.get().url("http://app2.gaofy.com:8080/gaofy/getSInfo/"
				+ BaseApplication.loginBean.data.userName
				+ "/14").build().execute(callback);
	}

	public void updateView(Double[] arr, int type) {
		if (ll_desc == null) {
			return;
		}
		ll_desc.setVisibility(View.VISIBLE);
		PentagonUtil ptg = new PentagonUtil(getActivity());
		ptg.drawPentagonWithPointArr(ptg.getPentagonWithPointArr(arr),
				mImageView_Wubianxing, R.drawable.pantage);
		switch (type) {
			case 0:
				//mTextView_Wubianxing.setText(zuhePinYu.getString());
				// if (null != ObjectCacheToFile.getCache("averageResultName",
				// String.class, getActivity())) {
				// mTextView_Wubianxing.setText(ObjectCacheToFile
				// .getCache("averageResultName", String.class,
				// getActivity()).toString());
				// } else {
				// mTextView_Wubianxing.setText("合格");
				// }
				if (zuhePinYu != null) {
					tv_cihui.setText(zuhePinYu.cihui);
					tv_cizu.setText(zuhePinYu.cizu);
					tv_shitai.setText(zuhePinYu.shitai);
					tv_juxing.setText(zuhePinYu.juxing);
					tv_lijie.setText(zuhePinYu.lijie);
				}
				else {
					ll_desc.setVisibility(View.INVISIBLE);
				}
				break;
			case 1:
				if (null != ObjectCacheToFile.getCache("pinyu", PinYu.class,
						getActivity())) {
					PinYu pinYu = (PinYu) ObjectCacheToFile.getCache("pinyu",
							PinYu.class, getActivity());
					//mTextView_Wubianxing.setText(pinYu.getString());
					//return "词汇:"+cihui+"\n词组:"+cizu+"\n时态:"+shitai+"\n句型:"+juxing+"\n理解:"+lijie;
					tv_cihui.setText(pinYu.cihui);
					tv_cizu.setText(pinYu.cizu);
					tv_shitai.setText(pinYu.shitai);
					tv_juxing.setText(pinYu.juxing);
					tv_lijie.setText(pinYu.lijie);
				} else {
					ll_desc.setVisibility(View.INVISIBLE);
					//tv_epmty.setText("暂未做题");
				}
				// if (null != ObjectCacheToFile.getCache("averageResultName",
				// String.class, getActivity())) {
				// mTextView_Wubianxing.setText(ObjectCacheToFile.getCache(
				// "localResultName", String.class, getActivity())
				// .toString());
				// } else {
				// mTextView_Wubianxing.setText("合格");
				// }
				break;
			default:
				break;
		}
	}

	@SuppressWarnings("deprecation")
	private void setData() {
		Date data = new Date();
		int day = data.getDate();
		mTextView_EveryDay.setText(AllContacts.everyDayTextArr[day - 1]);
		int everyDayImageId = getResources().getIdentifier("day" + day,
				"drawable", mContext.getApplicationInfo().packageName);
		if (everyDayImageId > 0) {
			mImageView_EveryDay.setImageResource(everyDayImageId);
		}
		mImageView_Notice.setImageUrl(
				"http://app.gaofy.com/upload/config/androidPriReading/mainurl.png",
				mImageLoader);

		OkHttpUtils.get()
				.url("http://app.gaofy.com/upload/config/androidPriReading/skill.json")
				.build().execute(new JSONCallback() {
			@Override
			public void onError(Call call, Exception e, int id) {

			}

			@Override
			public void onResponse(JSONObject response, int id) {
				String skill = response.optString("skill");
				tv_xxmj.setText(skill);
			}
		});
	}
}

